local ma__beizhi = fk.CreateSkill {
  name = "ma__beizhi",
}

Fk:loadTranslationTable{
  ["ma__beizhi"] = "悖志",
  [":ma__beizhi"] = "出牌阶段限一次，你可以与一名其他角色拼点，赢的角色须选择视为对包含没赢角色在内的三名角色使用一张【决斗】（不足则全选），且此牌造成伤害后，伤害来源获得受伤角色一张牌。",

  ["#ma__beizhi-duel"] = "悖志：视为对包含 %dest 在内的 %arg 名角色使用【决斗】",
  ["#ma__beizhi-get"] = "悖志：请选择获得 %dest 的一张牌",

  ["$ma__beizhi1"] = "",
  ["$ma__beizhi2"] = "",
}

ma__beizhi:addEffect("active", {
  anim_type = "offensive",
  card_num = 0,
  target_num = 1,
  can_use = function(self, player)
    return player:usedSkillTimes(ma__beizhi.name, Player.HistoryPhase) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and to_select ~= player and player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, ma__beizhi.name)
    local from, to
    if pindian.results[target].winner == player then
      from, to = player, target
    elseif pindian.results[target].winner == target then
      from, to = target, player
    else return false
    end
      local duel = Fk:cloneCard("duel")
      duel.skillName = ma__beizhi.name
      if from:prohibitUse(duel) then return end
      local targets = table.filter(player.room.alive_players, function(p)
        return not (p == from or p == to) and from:canUseTo(duel, p)
      end)
      local duel_target = {}
      if #targets ~= 0 then
        if #targets >= 3 then
          local tos = room:askToChoosePlayers(from, {
            min_num = math.min(#targets, 2),
            max_num = math.min(#targets, 2),
            targets = targets,
            skill_name = "ma__beizhi_active",
            prompt = "#ma__beizhi-duel::"..to.id..":"..math.min(#targets+1, 3),
            cancelable = false,
            extra_data = {
              ma__beizhi = from.id,
            },
            })
          if #tos > 0 then
            for _, p in ipairs(tos) do
              table.insert(duel_target, p)
            end
          end
        else 
          for _, p in ipairs(targets) do
            table.insert(duel_target, p)
          end
        end
      end
      table.insert(duel_target, to)
      room:useVirtualCard("duel", nil, from, duel_target, ma__beizhi.name, true)
  end,
})

ma__beizhi:addEffect(fk.Damage, {
  can_refresh = function(self, event, target, player, data)
    if data.card and data.card.trueName == "duel" and not data.from.dead and
      table.contains(data.card.skillNames, ma__beizhi.name) and not data.to.dead then
      local e = player.room.logic:getCurrentEvent().parent
      while e do
        if e.event == GameEvent.SkillEffect then
          local dat = e.data
          if dat.skill.name == ma__beizhi.name and dat.who == player then
            return true
          end
        end
        e = e.parent
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if data.to:isNude() then return end
    local card = room:askToChooseCard(data.from,{
        target = data.to,
        flag = "he",
        skill_name = ma__beizhi.name,
        prompt = "#ma__beizhi-get::"..data.to.id,
        cancelable = false,
      }
    )
    room:obtainCard(data.from, card, false, fk.ReasonPrey, data.from, ma__beizhi.name)
  end,
})

return ma__beizhi